package com.foo.common.base.config;

import javax.servlet.FilterRegistration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

import com.foo.common.base.listener.FooGenericListener;

/**
 * In Servlets 3.0 you don't need a web.xml for many cases, however, sometimes
 * it's required or just useful.
 *
 */
public class WebAppInitializer implements WebApplicationInitializer {

	Logger logger = LoggerFactory.getLogger(WebAppInitializer.class);

	public void onStartup(ServletContext container) throws ServletException {

		// Create the 'root' Spring application context
		AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
		rootContext.register(CoreConfig.class);

		// Add filter
		FilterRegistration struts2Filter = container
				.addFilter(
						"struts2",
						org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.class);
		struts2Filter.setInitParameter("actionPackages",
				"com.foo.common.base.action");
		struts2Filter.addMappingForUrlPatterns(null, false, "/*");

		// add Listener
		container.addListener(new ContextLoaderListener(rootContext));
		container.addListener(FooGenericListener.class);

	}

}